package org.example.volunteerend.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface SearchMapper {
    
    /**
     * 搜索项目
     */
    @Select("<script>SELECT project_id, project_name, description, cover_image_url, start_time, end_time, location, status " +
            "FROM volunteer_projects " +
            "WHERE (project_name LIKE CONCAT('%', #{keyword}, '%') " +
            "OR description LIKE CONCAT('%', #{keyword}, '%') " +
            "OR location LIKE CONCAT('%', #{keyword}, '%')) " +
            "ORDER BY start_time DESC " +
            "LIMIT #{offset}, #{pageSize}</script>")
    List<Map<String, Object>> searchProjects(
            @Param("keyword") String keyword,
            @Param("offset") Integer offset,
            @Param("pageSize") Integer pageSize);
    
    /**
     * 统计搜索项目总数
     */
    @Select("SELECT COUNT(*) FROM volunteer_projects " +
            "WHERE (project_name LIKE CONCAT('%', #{keyword}, '%') " +
            "OR description LIKE CONCAT('%', #{keyword}, '%') " +
            "OR location LIKE CONCAT('%', #{keyword}, '%'))")
    Integer countSearchProjects(@Param("keyword") String keyword);
    
    /**
     * 搜索用户
     */
    @Select("<script>SELECT user_id, username, real_name, avatar_url, total_service_hours " +
            "FROM users " +
            "WHERE (username LIKE CONCAT('%', #{keyword}, '%') " +
            "OR real_name LIKE CONCAT('%', #{keyword}, '%')) " +
            "ORDER BY total_service_hours DESC " +
            "LIMIT #{offset}, #{pageSize}</script>")
    List<Map<String, Object>> searchUsers(
            @Param("keyword") String keyword,
            @Param("offset") Integer offset,
            @Param("pageSize") Integer pageSize);
    
    /**
     * 统计搜索用户总数
     */
    @Select("SELECT COUNT(*) FROM users " +
            "WHERE (username LIKE CONCAT('%', #{keyword}, '%') " +
            "OR real_name LIKE CONCAT('%', #{keyword}, '%'))")
    Integer countSearchUsers(@Param("keyword") String keyword);
} 